function showproin() {
    $("#wntyName").val("");
    $("#wntyDesc").val("");
    $("#finishTime").val("");
    $(".hideproin").show();
};
//项目指标表单显示隐藏
function hideproin() {
    $(".hideproin").hide();
};
function showtarget() {
    $("#proTarget").val("");
    $("#tarFinishTime").val("");
    $(".tarhideproin").show();
};
function tarhideproin() {
    $(".tarhideproin").hide();
};
var stepId = $CertusJsonAjax.getStepInstanceId();
var processId = $CertusJsonAjax.getProInstanceId();
$CertusFormAjax.ajax("findLxInfo", { "processId": processId }, function (result) {
    var data = result.data.map;
    $$scope.proNo = data.pro_no;
    $$scope.proName = data.pro_name;
    var proType = data.pro_type;
    $$scope.isXS = false;
    if (proType == "ZL") {
        $$scope.proType = "战略类";
    } else if (proType == "YF") {
        $$scope.proType = "研发类";
    } else if (proType == "XS") {
        $$scope.isXS = true;
        $$scope.proType = "销售类";
    } else if (proType == "BM") {
        $$scope.proType = "部门类";
    } else if (proType == "SW") {
        $$scope.proType = "事务类";
    } else if (proType == "CS") {
        $$scope.proType = "测试类";
    }

    if (data.lx_level == "3") {
        $$scope.lxLevel = "高";
    } else if (data.lx_level == "2") {
        $$scope.lxLevel = "中";
    } else {
        $$scope.lxLevel = "低";
    }

    if (data.is_compare == "0") {
        $$scope.isCompare = "无";
    } else {
        $$scope.isCompare = "有";
    }

    if (data.urgency == 1) {
        $$scope.urgency = "紧急";
    } else {
        $$scope.urgency = "不紧急";
    }

    if (data.importance == 0) {
        $$scope.importance = "一般";
    } else if (data.importance == 1) {
        $$scope.importance = "重要";
    } else {
        $$scope.importance = "战略";
    }

    if (proType == "XS") {
        $$scope.showSign = true;
        $$scope.showHk = true;
        $$scope.showArea = true;
        $$scope.showCus = true;
        $$scope.showFinish = false;
        $$scope.showVersion = false;
        $$scope.showMoney = true;
        $$scope.showBudget = true;
        $$scope.showCtMoney = true;

        $$scope.proArea = data.pro_area;
        $$scope.cusName = data.cus_name;

        $$scope.divineSignDate = data.divine_sign_date;
        $$scope.divineHkDate = data.divine_hk_date;
        $$scope.selName = data.selName;
    } else if (proType == "ZL" || proType == "SW"
        || proType == "BM") {
        $$scope.showArea = false;
        $$scope.showCus = false;
        $$scope.showSign = false;
        $$scope.showHk = false;
        $$scope.showFinish = true;
        $$scope.showVersion = false;
        $$scope.showMoney = false;
        $$scope.showBudget = false;
        $$scope.showCtMoney = true;
        $$scope.divineFinishTime = data.divine_finish_time;
    } else {
        $$scope.showArea = false;
        $$scope.showCus = false;
        $$scope.showSign = false;
        $$scope.showHk = false;
        $$scope.showVersion = true;
        $$scope.showFinish = false;
        $$scope.showMoney = false;
        $$scope.showBudget = false;
        $$scope.divineVersionTime = data.divine_version_time
    }
    $$scope.clueDesc = data.clue_desc;
    $$scope.cusPolicy = data.cus_policy;
    $$scope.competitor = data.competitor;
    $$scope.lxDesc = data.lx_desc;
    $$scope.mgName = data.mgName;
    $$scope.pduName = data.pduName;
    $$scope.delName = data.delName;
    $$scope.startTime = data.start_time;
    $$scope.endTime = data.end_time;
    $$scope.deptNames = result.data.deptNames;
    if (data.pro_lv == "3") {
        $$scope.proLv = "高";
    } else if (data.pro_lv == "2") {
        $$scope.proLv = "中";
    } else {
        $$scope.proLv = "低";
    }

    $$scope.opinMsg = data.opinion_msg;

    $$scope.proId = data.pro_id;
    $$scope.lxId = data.id;

    $CertusFormAjax.ajax("getNlxRoles",{"company":data.comp_id},function (response) {
        var data = response.data;
        $$scope.rlList = data;
    });

    $CertusFormAjax.ajax("getProFiles",{},function (response) {
        var data = response.data;
        $$scope.wjList = data;
    });
});

//申请通过
$(".submit").on('click', function () {
    if (!certusValidateNotnull()) {
        return;
    }
    var arr = $("form").serializeArray();
    var data = {};
    for (var i = 0; i < arr.length; i++) {
        data[arr[i].name] = arr[i].value;
    };
    data.stepId = stepId;
    data.processId = processId;
    data.warnData = warnData;
    data.otherMoneyData = otherMoneyData;
    data.targetDate = targetDate;
    data.rlData = rlData;
    data.wjData = wjData;
    // 开启
    var index = layer.load(1);
    $CertusFormAjax.ajax('toWriteSwyc', { 'data': JSON.stringify(data) }, function () {
        //关闭
        layer.close(index);
        layer.msg("立项成功");
        window.history.back()
    });
});

// 立项信息-项目指标
var proinColumns = [{
    id: 'wntyName',
    title: '指标名称',
    width: 160,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'wntyDesc',
    title: '指标描述',
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'finishTime',
    title: '完成时间',
    type: 'string',
    width: 100,
    columnClass: 'text-left',
}, {
    id: 'operation',
    title: '操作',
    width: 70,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        content += '<button class="btn btn-xs btn-danger" onclick="deleteWarnA(' + record.wid + ');"><i class="fa fa-trash-o"></i>  删除</button>';
        return content;
    }
}];
var warnData = [];
//项目指标列表
var proinOption = {
    lang: 'zh-cn',
    ajaxLoad: false,
    extraWidth: 20,
    // loadURL:'url',
    exportFileName: '项目指标',
    datas: warnData,
    columns: proinColumns,
    gridContainer: 'proinContainer',
    toolbarContainer: 'proinToolBarContainer',
    tools: '',
    pageSize: 10,
    pageSizeLimit: [10, 15, 20]
};
var proinGrid = $.fn.dlshouwen.grid.init(proinOption);
proinGrid.load();
var warnI = 1;
$(".warnSubmit").on('click', function () {
    var wntyName = $("#wntyName").val();
    var wntyDesc = $("#wntyDesc").val();
    var finishTime = $("#finishTime").val();
    if (wntyName == "") {
        layer.msg("指标名称不能为空");
        return;
    }
    if (wntyDesc == "") {
        layer.msg("指标描述不能为空");
        return;
    }
    if (finishTime == "") {
        layer.msg("完成时间不能为空");
        return;
    }
    var warnArr = {};
    warnArr.wntyName = wntyName;
    warnArr.wntyDesc = wntyDesc;
    warnArr.finishTime = finishTime;
    warnArr.wid = warnI;
    warnData.push(warnArr);
    warnI++;
    proinGrid.load();
    $(".hideproin").hide();
});

function deleteWarn(wid) {

    layer.confirm('确认删除此数据？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        var dArr = {};
        for (var i = 0; i < warnData.length; i++) {
            if (warnData[i].wid == wid) {
                dArr = warnData[i];
            }
        }
        warnData.splice($.inArray(dArr, warnData), 1);
        proinGrid.load();
        layer.msg('删除成功');
    });


}

// 立项信息-项目目标
var prointColumns = [{
    id: 'proTarget',
    title: '项目目标',
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'finishTime',
    title: '完成时间',
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'operation',
    title: '操作',
    type: 'string',
    width: 70,
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        content += '<button class="btn btn-xs btn-danger" onclick="deleteTarget(' + record.wid + ');"><i class="fa fa-trash-o"></i>  删除</button>';
        return content;
    }
}];

var targetDate = [];
//项目目标列表
var prointOption = {
    lang: 'zh-cn',
    ajaxLoad: false,
    extraWidth: 20,
    // loadURL:'url',
    exportFileName: '项目目标',
    datas: targetDate,
    columns: prointColumns,
    gridContainer: 'targetContainer',
    tools: ''
};
var prointGrid = $.fn.dlshouwen.grid.init(prointOption);
prointGrid.load();
var tarid = 1;
$(".targetSubmit").on('click', function () {
    var proTarget = $("#proTarget").val();
    var finishTime = $("#tarFinishTime").val();
    if (proTarget == "") {
        layer.msg("项目目标不能为空");
        return;
    }
    if (proTarget.length > 120) {
        layer.msg("项目目标不能大于120字");
        return;
    }
    if (finishTime == "") {
        layer.msg("完成时间不能为空");
        return;
    }
    var targetArr = {};
    targetArr.proTarget = proTarget;
    targetArr.finishTime = finishTime;
    targetArr.wid = tarid;
    targetDate.push(targetArr);
    tarid++;
    prointGrid.load();
    $(".tarhideproin").hide();
});
function deleteTarget(wid) {
    var dArr = {};
    for (var i = 0; i < targetDate.length; i++) {
        if (targetDate[i].wid == wid) {
            dArr = targetDate[i];
        }
    }
    targetDate.splice($.inArray(dArr, targetDate), 1);
    prointGrid.load();
}


//项目其他费用表单显示隐藏
function hideother() {
    $(".hideother").hide();
};

function showother() {
    $(".hideother").show();
};

var otherColumns = [{
    id: 'comments',
    title: '事由',
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'bugetMoney',
    title: '预算金额',
    width: 150,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '￥' + value + '元';
        return content;
    }
},
{
    id: 'budgetBegin',
    title: '开始时间',
    type: 'string',
    width: 100,
    columnClass: 'text-center',
}, {
    id: 'budgetEnd',
    title: '结束时间',
    type: 'string',
    width: 100,
    columnClass: 'text-center',
},
{
    id: 'operation',
    title: '操作',
    width: 70,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record) {
        var content = '';
        content += '<button class="btn btn-xs btn-danger" onclick="deleteWarn(' + record.id + ');"><i class="fa fa-trash-o"></i>  删除</button>';
        return content;
    }
}];
var otherMoneyData = [];
var otherOption = {
    lang: 'zh-cn',
    ajaxLoad: false,
    extraWidth: 20,
    // loadURL:'url',
    exportFileName: '其他费用',
    datas: otherMoneyData,
    columns: otherColumns,
    gridContainer: 'otherContainer',
    tools: ''
};
var otherGrid = $.fn.dlshouwen.grid.init(otherOption);
otherGrid.load();

var om = 1;
function addOtherMoney() {
    var bugetMoney = $("#bugetMoney").val();
    var comments = $("#comments").val();
    var budgetBegin = $("#oStartTime").val();
    var budgetEnd = $("#oEndTime").val();
    if (bugetMoney == "") {
        layer.msg("预算金额不能为空");
        return;
    }
    if (budgetBegin == "" || budgetBegin == undefined || budgetBegin == null) {
        layer.msg("开始时间不能为空!");
        return;
    }
    if (budgetEnd == "" || budgetEnd == undefined || budgetEnd == null) {
        layer.msg("结束时间不能为空!");
        return;
    }


    if (comments == "") {
        layer.msg("事由不能为空");
        return;
    }
    var arr = {};
    arr.id = om;
    arr.bugetMoney = bugetMoney;
    arr.comments = comments;
    arr.budgetBegin = budgetBegin;
    arr.budgetEnd = budgetEnd;
    otherMoneyData.push(arr);
    om++;
    otherGrid.load();
    $(".hideother").hide();
}
function deleteWarn(wid) {
    layer.confirm('确认删除此数据？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        var dArr = {};
        for (var i = 0; i < otherMoneyData.length; i++) {
            if (otherMoneyData[i].id == wid) {
                dArr = otherMoneyData[i];
            }
        }
        otherMoneyData.splice($.inArray(dArr, otherMoneyData), 1);
        otherGrid.load();
        layer.msg('删除成功');
    });

}

function deleteWarnA(wid) {
    layer.confirm('确认删除此数据？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        var dArr = {};
        for (var i = 0; i < warnData.length; i++) {
            if (warnData[i].wid == wid) {
                dArr = warnData[i];
            }
        }
        warnData.splice($.inArray(dArr, warnData), 1);
        proinGrid.load();
        layer.msg('删除成功');
    });
}

this.showRl = function () {
    $(".hiderl").show();
}
this.showWj = function () {
    $(".wjhider").show();
};

var rlColumns = [{
    id: 'roleName',
    title: '角色',
    type: 'string',
    columnClass: 'text-center'
},{
    id: 'rtMoney',
    title: '级别',
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        if (value=="1"){
            content = "低";
        }else if (value=="2"){
            content = "中";
        }else{
            content = "高";
        }
        return content;
    }
},{
    id: 'beginTime',
    title: '开始时间',
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'endTime',
    title: '结束时间',
    type: 'string',
    columnClass: 'text-center',
},{
    id: 'rtNum',
    title: '人天',
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'comments',
    title: '说明',
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'operation',
    title: '操作',
    width: 75,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        content += '<button class="btn btn-xs btn-danger" onclick="deleteRl(' + record.rid + ');"><i class="fa fa-trash-o"></i>  删除</button>';
        return content;
    }
}];
var rlData = [];
var rlOption = {
    lang: 'zh-cn',
    ajaxLoad: false,
    extraWidth: 20,
    // loadURL:'url',
    exportFileName: '人力成本列表',
    datas: rlData,
    columns: rlColumns,
    gridContainer: 'rlContainer',
    toolbarContainer: 'rlToolBarContainer',
    tools: ''
};
var rlGrid = $.fn.dlshouwen.grid.init(rlOption);
rlGrid.load();

var rid = 1;
$(".rlSubmit").click(function () {
    var rArr = {};
    var userName = $('#rluserName option:selected').val();
    var userId = $("#rluserName").val();
    if (userId==""){
        layer.msg("请选择角色！");
        return;
    }
    var rtMoney = $("#rtMoney").val();
    var rlBeginTime = $("#rlBeginTime").val();
    var rlEndTime = $("#rlEndTime").val();
    if (rlBeginTime==""||rlEndTime==""){
        layer.msg("请输入使用周期！");
        return;
    }
    var rtNum = daysBetween(rlBeginTime,rlEndTime);
    var rlComments = $("#rlComments").val();
    rArr.roleName = userName;
    rArr.roleId = userId;
    rArr.rtMoney = rtMoney;
    rArr.beginTime = rlBeginTime;
    rArr.endTime = rlEndTime;
    rArr.comments = rlComments;
    rArr.rtNum = rtNum;
    rArr.rid = rid;
    //判断成员是否已经存在
    for(var i=0;i<rlData.length;i++){
        if (rlData[i].roleId==userId){
            layer.msg("成员已经存在！");
            return;
        }
    }
    rid++;
    rlData.push(rArr);
    rlGrid.load();
    $(".hiderl").hide();
    cleanRl()
});

//删除人力
function deleteRl(rid) {
    layer.confirm('确认删除此数据？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        var dArr = {};
        for (var i = 0; i < rlData.length; i++) {
            if (rlData[i].rid == rid) {
                dArr = rlData[i];
            }
        }
        rlData.splice($.inArray(dArr, rlData), 1);
        rlData.load();
        layer.msg('删除成功');
    });
}
function cleanRl() {
    $("#rluserName").val("");
    $("#rlBeginTime").val("");
    $("#rlEndTime").val("");
    $("#rlComments").val();
}
//人天预估 结束

//涉及文件
var wjColumns = [{
    id: 'wjName',
    title: '文件名称',
    type: 'string',
    columnClass: 'text-center'
}, {
    id: 'comments',
    title: '说明',
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'operation',
    title: '操作',
    width: 75,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        content += '<button class="btn btn-xs btn-danger" onclick="deleteWj(' + record.pwid + ');"><i class="fa fa-trash-o"></i>  删除</button>';
        return content;
    }
}];
var wjData = [];
var wjOption = {
    lang: 'zh-cn',
    ajaxLoad: false,
    extraWidth: 20,
    // loadURL:'url',
    exportFileName: '人力成本列表',
    datas: wjData,
    columns: wjColumns,
    gridContainer: 'wjContainer',
    toolbarContainer: 'wjToolBarContainer',
    tools: ''
};
var wjGrid = $.fn.dlshouwen.grid.init(wjOption);
wjGrid.load();
var pwid = 1;
$(".wjSubmit").click(function () {
    var wjId = $("#wjName").val();
    if (wjId==""){
        layer.msg("请选择文件！");
        return;
    }
    var wjName = $('#wjName option:selected').val();
    var comments = $("#wjComments").val();
    var arr = {};
    arr.pwid = pwid;
    arr.wjId = wjId;
    arr.wjName = wjName;
    arr.comments = comments;
    for(var i=0;i<wjData.length;i++){
        if (wjData[i].wjId==wjId){
            layer.msg("文件已经存在！");
            return;
        }
    }
    pwid++;

    wjData.push(arr);
    wjGrid.load();
    $(".wjhider").hide();
    cleanWj()
});

function deleteWj(pwid) {
    layer.confirm('确认删除此数据？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        var dArr = {};
        for (var i = 0; i < wjData.length; i++) {
            if (wjData[i].pwid == pwid) {
                dArr = wjData[i];
            }
        }
        wjData.splice($.inArray(dArr, wjData), 1);
        wjGrid.load();
        layer.msg('删除成功');
    });
}
function daysBetween(sDate1,sDate2){
    //Date.parse() 解析一个日期时间字符串，并返回1970/1/1 午夜距离该日期时间的毫秒数
    var time1 = Date.parse(new Date(sDate1));
    var time2 = Date.parse(new Date(sDate2));
    var nDays = Math.abs(parseInt((time2 - time1)/1000/3600/24));
    return  nDays;
};